package com.cpj.base.result;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.cpj.base.code.ErrorCode;
import com.cpj.base.exception.BaseErrorInfoInterface;

import java.io.Serializable;
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Result<D> implements Serializable{

    private String errorCode = ErrorCode.SUCCESS;
    private String errorMsg;
    private D data;

    private Integer totalCount;

    public static final Result SUCCESS = new Result();


    public Result(){
    }


    public String getErrorCode() {
        return errorCode;
    }

    public void setErrorCode(String errorCode) {
        this.errorCode = errorCode;
    }

    public String getErrorMsg() {
        return errorMsg;
    }

    public void setErrorMsg(String errorMsg) {
        this.errorMsg = errorMsg;
    }


    public D getData() {
        return data;
    }

    public void setData(D data) {
        this.data = data;
    }

    public Integer getTotalCount() {
        return totalCount;
    }

    public void setTotalCount(Integer totalCount) {
        this.totalCount = totalCount;
    }

    public static Result initResult(Object data){
        Result result = new Result<>();
        result.setData(data);
        return result;
    }
    public static Result initResult(){
        Result result = new Result<>();
        return result;
    }

    public static Result initResult(String errorMsg){
        Result result = Result.initResult();
        result.setErrorMsg(errorMsg);

        return result;
    }

    public static Result initResult(String errorCode,String errorMsg){
        Result result = Result.initResult(errorMsg);
        result.setErrorCode(errorCode);
        return result;
    }

    public static Result initResult(Object data, String errorCode){
        Result result = Result.initResult(data);
        result.setErrorCode(errorCode);
        return result;
    }

    public static Result initResult(Object data, Integer totalCount){
        Result result = Result.initResult(data);
        result.setTotalCount(totalCount);
        return result;
    }

    public static Result initErrorResult(String errorCode, String errorMsg){
        Result result = new Result<>();
        result.setErrorMsg(errorMsg);
        result.setErrorCode(errorCode);
        return result;
    }

    /**
     * 失败
     */
    public static Result initErrorResult(BaseErrorInfoInterface errorInfo) {
        Result result = new Result<>();
        result.setErrorMsg(errorInfo.getResultMsg());
        result.setErrorCode(errorInfo.getResultCode());
        return result;
    }
}
